<?
require(".././date.php");
require(".././connect.inc.php");
require('.././fonctions_bd.php');
require('.././fonctions.php');
require('../modelObject/presse_Obj.php');
require('../frameworks/JSON/JSON.php');
initdb();

$json = new Services_JSON();

$php_result = "";

	if(isset($_GET['type_requete'])) {
    $type_requete = $_GET['type_requete'];		
    //On identifie le type de requêtes
    
    /**
     *     Mise à jour de l'article
     */       
		if($type_requete == "update_article_presse") {
		  $type_update = $_GET['type_update'];
		  
		  
		  $article_presseObj = new ArticlePresse();
		  $article_presseObj->titre = html_entity_decode($_GET['titre_article_presse']);
		  $article_presseObj->url = $_GET['url_article_presse'];
		  
		  $query = ""; 
		  if ($type_update == "add_article_presse"){
        $query = "INSERT INTO `presse`(`id`, `titre`, `url`) VALUES ('', '$article_presseObj->titre', '$article_presseObj->url')";
      }else if ($type_update == "edit_article_presse"){
        $article_presseObj->idPresse = $_GET['id_article_presse']; 
        $query = "UPDATE `presse` SET `titre`='$article_presseObj->titre'
            , `url`='$article_presseObj->url' WHERE `id`=$article_presseObj->idPresse";
      }
			
			$queryResult = mysql_query($query);
		
			if (!$queryResult) {
	   			die('Requ?e "'.$query.'" invalide : ' . mysql_error());
			}else{
			// En cas de succès, on renvoit l'article_presse afin de compléter les informations
			  if ($type_update == "add_article_presse"){
        $query = "SELECT id FROM `presse` WHERE `titre` = '$article_presseObj->titre' 
                AND `url` = '$article_presseObj->url'";
        }else{
          $query = "SELECT id FROM `presse` WHERE `id`= '$article_presseObj->idPresse'";
        }
                
        $queryResult = mysql_query($query);
        
        if (!$queryResult) {
	   			die('Requ?e "'.$query.'" invalide : ' . mysql_error());
			  }
        
        if (mysql_numrows($queryResult) == 1){
          $article_presseObj->idPresse = mysql_result($queryResult,0,"id");
          
          // On prépare la réponse
          $article_presseObj->titre = htmlentities($article_presseObj->titre);
          $article_presseObj->url = $article_presseObj->texte;
          
          $php_result = $json->encode($article_presseObj); 
        }
        
        
      }
		}
		/**
     *  Récupération de l'article_presse
     */
    else if($type_requete == "get_article_presse") {
		  $id_art = $_GET['id_article_presse'];
		  
		  $queryResult = mysql_query("SELECT * FROM presse WHERE id=".$id_art);
	
	    $article_presseObj = new ArticlePresse();
		  $article_presseObj->idPresse = $id_art;
		  $article_presseObj->titre = htmlentities(mysql_result($queryResult,0,"titre"));
		  $article_presseObj->url = mysql_result($queryResult,0,"url");
		  
      
      $php_result = $json->encode($article_presseObj);
		}
		
			
		}
	
print $php_result;
?>
